﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using BUS;
using DTO;
using Telerik.Web.UI;

namespace TCV_WEB.Admin.Page
{
    public partial class NewsCategories : System.Web.UI.Page
    {
        string strActivity = "";
        string strMenuProductID = "";
        string strLanguageID = "EN";
        public string Username;
        public static int ContentLength;
        string _sErr = "";
        int _Rank;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UsernameAdmin"] == null)
            {
                Response.Redirect("~/Admin/Login.aspx");
            }
            else
            {
                Username = Session["UsernameAdmin"].ToString();
            }
            if (!Page.IsPostBack)
            {
                // pro = new ProductType(strLanguageID);
                reset();
                this.LoadDataGrid();
                //this.lblManageTypeProduct.Text = CommonTask.GetFormTitle("ManageProductTitle");
            }

        }

        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            reset();
        }
        protected void reset()
        {
            txtnewnameEn.Text = "";
            txtnewname.Text = "";
            txtNewsCatID.Text = "";
            txtRank.Text = "";
            rblIsViewed.Items[0].Selected = true;
            Session["IDCategoryNews"] = null;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            NewsCategoriesInfo tmp = new NewsCategoriesInfo();
            try
            {
                if (Session["IDCategoryNews"] != null)
                {
                    strActivity = "Update";
                    tmp.ID = Convert.ToInt32(txtNewsCatID.Text);
                }
                else
                {
                    strActivity = "Insert";
                    //pro.MenuProductID = "";

                }

                tmp.NameVN =  txtnewname.Text;
                tmp.Name = txtnewnameEn.Text;
                tmp.IDStatus = Convert.ToInt32(rblIsViewed.SelectedValue);

                try { tmp.Rank = Convert.ToInt32(txtRank.Text); }
                catch { tmp.Rank = 0; }
                NewsCategoriesControl newcat_Control = new NewsCategoriesControl();
                if (strActivity == "Update")
                {
                    _sErr = newcat_Control.Update(tmp);
                }
                else if (strActivity == "Insert")
                {
                    newcat_Control.Add(tmp, ref _sErr);
                }
            }
            catch (Exception ex)
            {
                lblErr.Text = ex.Message.ToString();
            }
            this.LoadDataGrid();
            reset();
        }

        private void LoadDataGrid()
        {
            DataTable dtList = new DataTable();
            try
            {
                DataTable dtb;
                NewsCategoriesControl newcats_control = new NewsCategoriesControl();

                dtb = newcats_control.GetAll(ref _sErr);
                dgvList.DataSource = dtb;
            //    dgvList.DataBind();
                
            }
            catch (Exception ex)
            {
                lblErr.Text = ex.Message;
            }
            finally
            {
                dtList.Dispose();
            }
        }

        protected void dtgrid_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            //     dtgrid.CurrentPageIndex = e.NewPageIndex;
            //    this.LoadDataGrid();
        }

        protected void dtgrid_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            //if (e.CommandName.ToUpper() == "EDIT")
            //{
            //    int inewscatid = Convert.ToInt32(dtgrid.Items[e.Item.ItemIndex].Cells[0].Text.ToString());
            //    this.LoadData(inewscatid);
            //    //Session["ProductID"] = strProductID;
            //}
            //if (e.CommandName.ToUpper() == "DETAIL")
            //{
            //    int inewscatid = Convert.ToInt32(dtgrid.Items[e.Item.ItemIndex].Cells[0].Text.ToString());
            //    Session["newcatid"] = inewscatid;
            //    Response.Redirect("~/Admin/Page/News.aspx");
            //}
        }

        private void LoadData(int inewcatID)
        {
            //pro = new MenuProduct(strLanguageID);
            //pro.MenuProductID = strProductID;
            //DataRow dr = pro.GetbyID();
            NewsCategoriesControl newscat_Control = new NewsCategoriesControl();
            NewsCategoriesInfo tmp = newscat_Control.Get(inewcatID, ref _sErr);
            txtNewsCatID.Text = tmp.ID.ToString();
            txtnewname.Text = tmp.NameVN.ToString();
            txtnewnameEn.Text = tmp.Name.ToString();
           
            rblIsViewed.SelectedValue = tmp.IDStatus.ToString();
            txtRank.Text = tmp.Rank.ToString();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                string strID = "";
                //for (int i = 0; i < dgvList.SelectedItems.Count; i++)
                //{

                //    strID += dgvList.SelectedItems[i].OwnerTableView.DataKeyValues[dgvList.SelectedItems[i].RowIndex]["ID"].ToString() + ",";

                //}
                if (Session["IDCategoryNews"] != null)
                {

                    NewsCategoriesControl newcat_Control = new NewsCategoriesControl();
                    newcat_Control.Delete_NewsCategories(Session["IDCategoryNews"].ToString());
                    dgvList.Rebind();

                    this.reset();
                }
            }
            catch (Exception ex)
            {
           //     lblErr.Text = ex.Message;
            }

        }

        protected void dtgrid_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {

            //NewsCategoriesControl newcats_control = new NewsCategoriesControl();
            //DataTable dtb = newcats_control.GetAll_NewsCategories(ref _sErr);

            //RadGrid1.DataSource = dtb;
        }

        protected void RadGrid1_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {

        }

        protected void RadGrid1_DeleteCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {

        }

        protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item is GridDataItem && e.Item.DataItem != null)
            {
                DataRow row = ((DataRowView)e.Item.DataItem).Row;
                //Label sttCtrl = (Label)e.Item.FindControl("colSTT");
                //sttCtrl.Text = (e.Item.ItemIndex + 1).ToString("00");
                Int32 ID = Convert.ToInt32(row["ID"]);
                NewsCategoriesControl newsCtrl = new NewsCategoriesControl();
                NewsCategoriesInfo newsInfo = newsCtrl.Get(ID, ref _sErr);
                Label proName = (Label)e.Item.FindControl("IDStatus");
                if (newsInfo.IDStatus == 1)
                    proName.Text = "Sử dụng";
                else
                    proName.Text = "Không sử dụng";

                //Image proImage = (Image)e.Item.FindControl("colProductImage");
                //proImage.ImageUrl = "~/Uploads/Image/" + proInfo.Image;

                //Label total = (Label)e.Item.FindControl("colAmount");
                //double price = Convert.ToDouble(row["Price"]);
                //int quantity = Convert.ToInt32(row["Quantity"]);
                //total.Text = Convert.ToDouble(price * quantity).ToString("#,##0.##");
            }
        }

        protected void dgvList_ItemCommand(object source, GridCommandEventArgs e)
        {
            int iidcatenews = -1;
            if (e.CommandName.ToUpper() == "DETAIL")
            {
                iidcatenews = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"].ToString().Trim());
                Session["IDCategoryNews"] = iidcatenews;
                Response.Redirect("~/Admin/Page/News.aspx");
                //Session["ProductID"] = strProductID;
            }
            else if (e.CommandName.ToUpper() == "LOAD")
            {
                iidcatenews = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"].ToString().Trim());
                e.Item.Selected = true;
                Session["IDCategoryNews"] = iidcatenews;
                LoadData(iidcatenews);
            }
        }

        protected void dgvList_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem && e.Item.DataItem != null)
            {
                try
                {
                    DataRow row = ((DataRowView)e.Item.DataItem).Row;
                    Int32 ID = Convert.ToInt32(row["ID"]);
                    NewsCategoriesControl newsCtrl = new NewsCategoriesControl();
                    NewsCategoriesInfo newsInfo = newsCtrl.Get(ID, ref _sErr);
                    Label proName = (Label)e.Item.FindControl("IDStatus");
                    if (newsInfo.IDStatus == 1)
                        proName.Text = "Sử dụng";
                    else
                        proName.Text = "Không sử dụng";
                }
                catch (Exception ex)
                { }

            }
        }

        protected void dgvList_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            LoadDataGrid();
        }
    }
}
